<!DOCTYPE html>
<html>
    <head>
        <title>Pivot Demo with R Datasets</title>
        <script>
          (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
          (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
          m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
          })(window,document,'script','https://www.google-analytics.com/analytics.js','ga');

          ga('create', 'UA-46914903-2', 'auto');
          ga('send', 'pageview');

        </script>
        <!-- external libs from cdnjs -->
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.11/c3.min.css">
        <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.min.css">
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jqueryui-touch-punch/0.2.3/jquery.ui.touch-punch.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/PapaParse/4.1.2/papaparse.min.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/chosen/1.4.2/chosen.jquery.js"></script>
        <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/c3/0.4.18/c3.min.js"></script>

        <!-- PivotTable.js libs from ../dist -->
        <link rel="stylesheet" type="text/css" href="../dist/pivot.css">
        <script type="text/javascript" src="../dist/pivot.js"></script>
        <script type="text/javascript" src="../dist/d3_renderers.js"></script>
        <script type="text/javascript" src="../dist/c3_renderers.js"></script>
        <script type="text/javascript" src="../dist/export_renderers.js"></script>

        <style>
            body {font-family: Verdana;}
            .node {
              border: solid 1px white;
              font: 10px sans-serif;
              line-height: 12px;
              overflow: hidden;
              position: absolute;
              text-indent: 2px;
            }
            .c3-line, .c3-focused {stroke-width: 3px !important;}
            .c3-bar {stroke: white !important; stroke-width: 1;}
            .c3 text { font-size: 12px; color: grey;}
            .tick line {stroke: white;}
            .c3-axis path {stroke: grey;}
            .c3-circle { opacity: 1 !important; }
            .c3-xgrid-focus {visibility: hidden !important;}
        </style>
    </head>
    <body>
        <script type="text/javascript">
            $(function(){
                Papa.parse("https://raw.githubusercontent.com/nicolaskruchten/Rdatasets/master/datasets.csv", {
                    download: true,
                    header: true,
                    skipEmptyLines: true,
                    complete: function(parsed){
                        var csvlist_arr = parsed.data;
                        var pkg = $("<optgroup>", {label: ""});
                        for(var i in csvlist_arr)
                        {
                            var dataset = csvlist_arr[i];
                            if(dataset.Package != pkg.attr("label"))
                            {
                                pkg = $("<optgroup>", {label: dataset.Package}).appendTo($("#csv"));
                            }
                            pkg.append($("<option>", {value: dataset.Package+"/"+dataset.Item}).text(dataset.Item +":" +dataset.Title));
                        }
                        $("#csv").chosen();
                        var renderers = $.extend(
                            $.pivotUtilities.renderers,
                            $.pivotUtilities.c3_renderers,
                            $.pivotUtilities.d3_renderers,
                            $.pivotUtilities.export_renderers
                            );
                        $("#csv").bind("change", function(event){
                            $("#output").empty().text("Loading...")
                            var val = $(this).val();
                            Papa.parse("https://raw.githubusercontent.com/nicolaskruchten/Rdatasets/master/csv/"+val+".csv", {
                                download: true,
                                skipEmptyLines: true,
                                complete: function(parsed){
                                $("#doc").empty().append(
                                    $("<a>",{target:"_blank", href:"http://nicolas.kruchten.com/Rdatasets/doc/"+val+".html"}).html("Dataset documentation &raquo;")
                                    );
                                    $("#output").pivotUI(parsed.data, {
                                        hiddenAttributes: [""],
                                        renderers: renderers }, true);
                                }
                            });
                        });
                    }
                });
            });
        </script>
        <p><a href="index.html">&laquo; back to PivotTable.js examples</a></p>
        <p><a href="https://github.com/vincentarelbundock/Rdatasets">Rdatasets</a> is a collection of 758 datasets that were originally distributed alongside the statistical software environment R and some of its add-on packages.</p>
        <p>R Dataset: <select id="csv"  data-placeholder="Choose a dataset..."><option></select> &nbsp; &nbsp; <span id="doc"></span></p>
        <div id="output" style="margin: 10px;"></div>

    </body>
</html>
